package com.iceland.common.thread;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.MarkerFactory;

import java.util.concurrent.*;

/**
 * 这个线程池为每个任务开启新线程，适用于业务逻辑独占线程的场景
 */
@Slf4j
public class MessageEventExcutor {

    private volatile static int corePoolSize=4;
    private volatile static int maxPoolSize=150;
    private volatile static ThreadPoolExecutor threadPoolExecutor;
    protected MessageEventExcutor(){

    }

    public static int getCorePoolSize() {
        return corePoolSize;
    }

    public static void setCorePoolSize(int corePoolSize) {
        MessageEventExcutor.corePoolSize = corePoolSize;
    }

    public static int getMaxPoolSize() {
        return maxPoolSize;
    }

    public static void setMaxPoolSize(int maxPoolSize) {
        MessageEventExcutor.maxPoolSize = maxPoolSize;
    }

    public synchronized static ThreadPoolExecutor getInstance(){
        if(threadPoolExecutor!=null){
            return threadPoolExecutor;
        }
        synchronized (MessageEventExcutor.class){
            if(threadPoolExecutor==null){
                threadPoolExecutor=new ThreadPoolExecutor(corePoolSize,maxPoolSize,10, TimeUnit.SECONDS,new SynchronousQueue<>(),new MessageThreadFactory("MessageEventExcutor"));
                log.info(MarkerFactory.getMarker(MessageEventExcutor.class.getTypeName()),"创建线程池MessageEventExcutor，corePoolSize:{},maxPoolSize:{}",corePoolSize,maxPoolSize);
            }
        }
        return threadPoolExecutor;
    }

}


